package com.fengmi.pay.listener;

import com.api.order.OrderApi;
import com.fengmi.order.entity.OmsOrder;
import com.fengmi.order.entity.OmsOrderItem;
import com.fengmi.pay.service.IPayService;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RocketMQMessageListener(consumerGroup = "pay-delay-group",
        topic = "pay-delay",selectorExpression = "pay-delay-tag",
        consumeMode = ConsumeMode.CONCURRENTLY,
        messageModel = MessageModel.CLUSTERING)
public class PayDelayListener implements RocketMQListener<String> {


    @Autowired
    private IPayService payService;

    @Autowired
    private OrderApi orderApi;

    @Override
    public void onMessage(String ordersn) {


        //1:查询订单的支付状态
        String payStatus = payService.queryStatus(ordersn);

        if("NOTPAY".equals(payStatus)){

            //2:关闭订单
            String closeorder = payService.closeorder(ordersn);

            if("SUCCESS".equals(closeorder)){
                //3:释放库存
                OmsOrder order = orderApi.findOrderInfoByOrdersn(ordersn);

                List<OmsOrderItem> itemList = order.getItemList();

                itemList.forEach(item->{
                    Long productSkuId = item.getProductSkuId();
                    Integer buyNum = item.getProductQuantity();
                    System.out.println();
                    //归还库存


                });


            }

        }



    }
}
